package com.fbli.service.Impl;

import com.fbli.mapper.GoodsOrderMapper;
import com.fbli.mapper.UserMapper;
import com.fbli.pojo.GoodsOrder;
import com.fbli.service.GoodsOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class GoodsOrderServiceImpl implements GoodsOrderService {
    @Autowired
    private GoodsOrderMapper goodsOrderMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void insert(GoodsOrder record) {
        System.out.println("insertService");
        record.setStatus(1);//设置支付状态，1表示已支付
        Timestamp time2 = new Timestamp(new Date().getTime());
        record.setCreateTime(time2);//设置订单创建时间
        record.setPaymentTime(time2);//设置付款时间
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(username);
        Integer userId = userMapper.selectUserId(username);
        record.setUserid(userId);
        System.out.println(record.toString());
        int i = goodsOrderMapper.insert(record);
        System.out.println(i);
    }

    @Override
    public void deleteByPrimaryKey(long[] orderids) {
        for (long orderid:orderids){
            goodsOrderMapper.deleteByPrimaryKey((int) orderid);
        }
    }

    @Override
    public void updateByPrimaryKey(GoodsOrder record) {

        Timestamp time = new Timestamp(System.currentTimeMillis());
        record.setReleaseTime(time);
        goodsOrderMapper.updateByPrimaryKey(record);
    }

    @Override
    public GoodsOrder selectByPrimaryKey(int orderid) {

        return goodsOrderMapper.selectByPrimaryKey(orderid);

    }

    @Override
    public List<GoodsOrder> selectAll() {
        return goodsOrderMapper.selectAll();
    }

    @Override
    public List<GoodsOrder> selectOrder(String keyword) {
        return goodsOrderMapper.selectOrder(keyword);
    }

    @Override
    public PageInfo<GoodsOrder> selectPage(GoodsOrder goodsOrder, int pageNum, int pageSize) {
        System.out.println("1111111");
        //分页开始
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsOrder> goodsOrders = goodsOrderMapper.selectCondition(goodsOrder);
        //返回分页后的结果
        PageInfo<GoodsOrder> goodsOrdersPageInfo = new PageInfo<>(goodsOrders);
        return goodsOrdersPageInfo;
    }


}
